/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import controller.FilesController;
import controller.LoginController;
import controller.RootsController;
import domain.EccezioneUpdater;
import domain.Root;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MainView extends JFrame{
    private static final MainView instance = new MainView();
    
    private JPanel currentPanel = null;
    
    public static MainView getInstance(){
        return instance;
    }
    
    private MainView(){
        super("Login");
        GridLayout layout = new GridLayout(1, 1);
        setLayout(layout);
        showLoginPanel();
        pack();
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(getSize().height + 250, getSize().width + 250);
        setMinimumSize(getSize());
    }

    private void showLoginPanel() {
        LoginView view = new LoginView();
        LoginController.getInstance().setView(view);
        setCurrentView(view);
    }
    
    public void loginDone() {
        try {
            RootsView view = new RootsView();
            RootsController.getInstance().setView(view);
            setCurrentView(view);
            setTitle("Selezione root");
        } catch (EccezioneUpdater ex) {
            GUIUtils.showError(ex.getMessage());
            showLoginPanel();
        }
    }
    
    private void setCurrentView(JPanel p){
        if(currentPanel != null)
            remove(currentPanel);
        currentPanel = p;
        add(currentPanel);
        repaint(1000);
        validate();
    }

    public void selected(Object obj) {
        Root root = (Root)obj;
        FilesView filesPanel = new FilesView();
        FilesController.getInstance().setView(filesPanel);
        FilesController.getInstance().fillFiles(root);
        setCurrentView(filesPanel);
        setTitle("Root: " + root.getPath());
        setExtendedState(JFrame.MAXIMIZED_BOTH);
        repaint();
        validate();
    }
}
